<?php


namespace app\cms\model\merchant;


use app\cms\community\lib\QueryModel;
use app\cms\dto\merchant\merchantLicenseInfo;
use iflow\Response;

class merchantLicenseModel extends QueryModel
{

    protected $table = "merchant_license_table";


    public function hasMerchant(): \think\model\relation\HasOne
    {
        return $this->hasOne(merchantModel::class, 'merchant_id', 'merchant_id');
    }


    public function licenseList(
        // 商户id
        int $merchant_id = 0,
        // 许可id
        string $merchant_license_id = "",
        // 许可类型
        int $license_type = -1,
        // 许可审核状态
        int $license_review_type = -1,
        // 商户名称
        string $merchant_name = ""
    ): Response {
        $where = [];
        if ($merchant_id > 0) {
            $where['merchant_id'] = $merchant_id;
        }

        if ($merchant_license_id != "") {
            $where['merchant_license_id'] = $merchant_license_id;
        }

        if ($license_type > -1) {
            $where['license_type'] = $license_type;
        }

        if ($license_review_type > -1) {
            $where['license_review_type'] = $license_review_type;
        }

        $query = $this->withJoin([
            'hasMerchant' => [
                'uid' => 'uid',
                'merchant_name' => 'merchant_name',
                'merchant_logo' => 'merchant_logo',
                'merchant_type' => 'merchant_type'
            ]
        ], 'LEFT') -> where($where);

        if ($merchant_name !== '') {
            $query -> whereLike('merchant_name', "%${merchant_name}%");
        }

        // 查询单条数据
        if ($merchant_id > 0) {
            $info = $query -> findOrEmpty();
            if ($info -> isEmpty()) return message() -> nodata('暂无许可数据');
            return message() -> success('查询数据成功', $info);
        }

        $query = $query -> paginate([
            'page' => request() -> getParams('page'),
            'list_rows' => 10
        ]);

        $items = $query -> items();
        return $items ?
            message()->success('查询成功', $items, [
                'count' => $query->total(),
                'page' => $query->lastPage()
            ]) : message()->nodata('暂无数据');
    }

    /**
     * 创建许可证书
     * @param merchantLicenseInfo $merchantLicenseInfo
     * @return Response
     */
    public function createLicense(merchantLicenseInfo $merchantLicenseInfo): Response
    {
        if (
            $this->where([
                'license_number' => $merchantLicenseInfo -> license_number
            ]) -> findOrEmpty() -> isExists()
        ) return message() -> parameter_error('许可证书编号已存在');

        $data = self::create([
            'uid' => $merchantLicenseInfo -> getUid(),
            'merchant_id' => $merchantLicenseInfo -> getMerchantId(),
            'license_file' => $merchantLicenseInfo -> license_file,
            'legal_person' => $merchantLicenseInfo -> legal_person,
            'license_number' => $merchantLicenseInfo -> license_number,
            'license_type' => $merchantLicenseInfo -> license_type
        ]) -> toArray();

        return message() -> success('提交成功， 等待审核中', $data);
    }


    /**
     * @param merchantLicenseInfo $merchantLicenseInfo 商户证书数据
     * @param string $type 更新类型 up_review 更新审核状态为待审核
     * @return Response
     */
    public function updateLicense(merchantLicenseInfo $merchantLicenseInfo, string $type): Response
    {
        $update = [
            'license_file' => $merchantLicenseInfo -> license_file,
            'legal_person' => $merchantLicenseInfo -> legal_person,
            'license_number' => $merchantLicenseInfo -> license_number,
            'license_type' => $merchantLicenseInfo -> license_type
        ];

        if ($type === 'up_review') {
            $update['license_review_type'] = 0;
        }

        if ($merchantLicenseInfo -> merchant_license_id === "") {
            return message() -> parameter_error("请选择需要更新的许可证");
        }

        self::update($update, [
            'uid' => $merchantLicenseInfo -> getUid(),
            'merchant_id' => $merchantLicenseInfo -> getMerchantId(),
        ]);
        return message() -> success('更新成功');
    }

    public function deleteLicense($merchant_id, $merchant_license_id): Response
    {
        return $this-> where([
            'merchant_id' => $merchant_id,
            'merchant_license_id' => $merchant_license_id
        ]) -> delete() ? message() -> success('删除成功'):
            message() -> update_failed('删除失败');
    }

    // 更新审核状态
    public function reviewMerchantLicense(
        int $merchant_id,
        int $merchant_license_id,
        int $license_review_type,
        string $license_review_desc
    ): Response {
        $update = self::update([
            'license_review_type' => $license_review_type,
            'license_review_desc' => $license_review_desc,
        ], [
            'merchant_id' => $merchant_id,
            'merchant_license_id' => $merchant_license_id,
        ]);
        return message() -> success('更新成功', $update -> getChangedData());
    }
}